package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @Author : xiongke
 * @Description:
 * @Date: 2024/8/28
 */
@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Resource
    SetmealMapper setmealMapper;
    @Resource
    SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 保存套餐
     *
     * @param dto DTO
     */
    @Override
    @Transactional
    public void save(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        //插入setmeal
        setmealMapper.save(setmeal);
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        //插入setmealDish
        if (!setmealDishes.isEmpty()) {
            Long id = setmeal.getId();
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(id);
            }
            setmealDishMapper.save(setmealDishes);
        }
    }

    @Override
    public PageResult queryPage(SetmealPageQueryDTO dto) {
        // 分页查询
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<SetmealVO> page = setmealMapper.queryPage(dto);
        long total = page.getTotal();
        List<SetmealVO> result = page.getResult();
        return new PageResult(total, result);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        // 判断将要起售的套餐中的菜品是否已停售
        if (status == StatusConstant.ENABLE) {
            List<Dish> dishIdList = setmealDishMapper.queryDishBySetmealId(id);
            if (!dishIdList.isEmpty()) {
                for (Dish dish : dishIdList) {
                    if (dish.getStatus() == StatusConstant.DISABLE) {
                        throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                }
            }
        }
        Setmeal setmeal = Setmeal.builder().status(status).id(id).build();
        setmealMapper.updateByCondition(setmeal);
    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.getById(id);
            if(StatusConstant.ENABLE == setmeal.getStatus()){
                //起售中的套餐不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

        ids.forEach(setmealId -> {
            //删除套餐表中的数据
            setmealMapper.deleteById(setmealId);
            //删除套餐菜品关系表中的数据
            setmealDishMapper.deleteBySetmealId(setmealId);
        });

    }

    @Override
    public SetmealVO getSetmealById(Long id) {
        SetmealVO result = new SetmealVO();
        Setmeal setmeal = setmealMapper.getById(id);
        BeanUtils.copyProperties(setmeal,result);
        //查询套餐关联的菜品 List<SetmealDish> setmealDishes
        List<SetmealDish> setmealDishes =  setmealDishMapper.queryBySetmealId(id);
        result.setSetmealDishes(setmealDishes);
        return result;
    }

    @Override
    @Transactional
    public void updateByCondition(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        //更新套餐表
        setmealMapper.updateByCondition(setmeal);
        //更新套餐对应菜品表
        Long id = setmeal.getId();
        //先删除再插入
        setmealDishMapper.deleteBySetmealId(id);
        //插入
        List<SetmealDish> setmealDishList = dto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishList) {
            setmealDish.setSetmealId(id);
        }
        setmealDishMapper.save(setmealDishList);

    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
